<?php
/**
 * Created by PhpStorm.
 * User: 吕金鹏
 * Date: 2021/6/11
 * Time: 5:44 下午
 */

namespace app\index\controller;
use think\Db;
use think\facade\Request;

class Keti extends Base
{
    public function klist(){
        
        $nian = input('nian');
        if(!$nian){
            $list = Db::name("keti")->select();
            $ary =[];
            foreach($list as $val){
                $ary[] = $val['year'];
            }
            $ary = array_unique($ary);

            $this->assign("nian",$ary);
            return $this->fetch("chosenian");
        }
        
        if($this->admin['area']==52){
            $cate = "直属校";
        }elseif($this->admin['area']==2){
            $cate = "普通高校、高职院校";
        }else{
            $cate = "其他中小学/中职/特教学校";
        }
        $klist = Db::name('member_class as mc')
                ->join("keti k",'mc.cid=k.id')
                ->where('k.status',1)
                ->where('mc.status',1)
                ->where('mc.type',$cate)
                ->where("k.year",$nian)
                ->order('year desc')
                ->paginate(9);
        $this->assign('nian',$nian);
        $this->assign("data",$klist);
        $this->assign('page',$klist->render());
        return $this->fetch();
    }
    public function klist1(){
        if($this->admin['area']==52){
            $cate = "直属校";
        }elseif($this->admin['area']==2){
            $cate = "普通高校、高职院校";
        }else{
            $cate = "其他中小学/中职/特教学校";
        }
        $klist = Db::name('member_class as mc')
                ->join("keti k",'mc.cid=k.id')
                ->where('k.status',1)
                ->where('mc.status',1)
                ->where('mc.type',$cate)
                ->order('year desc')
                ->paginate(9);

        $this->assign("data",$klist);
        $this->assign('page',$klist->render());
        return $this->fetch();
    }
    public function info(){
        $id = input("id");
        
        $info = Db::name("keti")->where("id",$id)->find();
        
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function shenbao(){
        $id= input('id');
        $if_exit = Db::name("shenqing")->where("admin_id",$this->admin['id'])->where('cid',$id)->where('if_re',1)->find();
        if($if_exit){
            $this->error("课题申请已被禁止提交",'/index/keti/shenqinglist?id='.$if_exit['id']);
        }
        //查看是否已经申请
        $if_exit = Db::name('shenqing')->where("admin_id",$this->admin['id'])->where('cid',$id)->where('status','not in','2,5,7')->find();
        if($if_exit){
            $this->error("课题申请已提交，请等待审核",'/index/keti/shenqinglist?id='.$if_exit['id']);
        }
        
        if(Request::isPost()){
            if($this->admin['rid']==13){
                ajaxMsg(0,'没有权限');
            }
            $data = input("post.");
            // dump($this->admin);die;
            $kt_info = Db::name("keti")->where("id",$data['cid'])->find();
           
            //生成word
            $data_sq['if_inportant'] = $data['if_inportant'];
            $data_sq['content'] = json_encode($data);
            $data_sq['time'] = time();
            $data_sq['cid'] = $data['cid'];
            $data_sq['name'] = $data['name'];
            $data_sq['sex'] = $data['sex'];
            $data_sq['zhiwu'] = $data['zhiwu'];
            $data_sq['bumen'] = $data['bumen'];
            $data_sq['if_teacher'] = $data['if_teacher'];
            $data_sq['if_two'] =$data['if_two'];
            //判断所属学校,进行审批提交
             //1 学校待审核 2 学校拒绝 3 中心待审核 4 教委待审核 5 教委拒绝 6 审核通过 7 中心审核拒绝 
            if($this->admin['area']==52){
                $cate = "直属校";
                $data_sq['status'] = 1;
                
            }elseif($this->admin['area']==2){
                $cate = "普通高校、高职院校";
                $data_sq['status'] = 1;
            }else{
                $cate = "其他中小学/中职/特教学校";
                $data_sq['status'] = 4;
            }
            // dump($this->admin);die;
            $data_sq['school_id'] = $this->admin['school'];
            //查询是否名额已经满员
            // $member_class = Db::name("member_class")->where("cid",$data['cid'])->where("type",$cate)->find();
            // if($data_sq['school_id']){
            //     $count = DB::name("shenqing")->where("school_id",$data_sq['school_id'])->where('cid',$data['cid'])->count();
            // }else{
            //     $count = DB::name("shenqing")->where("area",$this->admin['area'])->where('school','')->where('cid',$data['cid'])->count();
            // }
            
            
            // if($member_class['num'] <=$count){
            //     ajaxMsg(0,'该课题申报人已满');
            // }
           
            $baocun = input("baocun");
            if($baocun==1){
                $data_sq['status'] = -1;
            }                    
            $data_sq['admin_id'] = $this->admin['id'];
            // dump($data_sq);die;
            $res = Db::name('shenqing')->insert($data_sq);
            if($res){
                ajaxMsg(1,'提交成功');
            }
            ajaxMsg(0,'网络错误,稍后再试');
        }
        // $kt_info = Db::name("keti")->where("id",$id)->find();
        $this->assign("id",$id);
        $if_two = input('if_two');
        // echo $if_two;die;
        $this->assign('admin',$this->admin);
        $this->assign("if_two",$if_two);
        return $this->fetch();
    }
    public function shenqinglist(){
        $status = input('status');
        $where = [];
        if($status){
            $where['sq.status'] = $status;
        }
        $list = Db::name("shenqing as sq")
                ->join("keti kt",'sq.cid=kt.id')
                ->where("sq.admin_id",$this->admin['id'])
                ->where($where)
                ->order("sq.time desc")
                ->field("kt.name,sq.if_inportant,sq.count1,sq.count2,sq.message,sq.status,sq.id,sq.time,sq.if_re")
                ->paginate(10);
        $this->assign('page',$list->render());
        //根据学校种类.生成审核部门
        $title = [];
        
        if($this->admin['area']==52){
                $cate = "直属校";
                // $data_sq['status'] = 3;
                
        }elseif($this->admin['area']==2){
                $cate = "普通高校、高职院校";
                // $data_sq['status'] = 1;
        }else{
                $cate = "其他中小学/中职/特教学校";
                // $data_sq['status'] = 4;
        }
        if($cate=="普通高校、高职院校"){
            $title[0] = '学校审核';
            $title[1] = '中心审核';
            $ntitle[0] = '学校审核';
            $ntitle[1] = '市中心审核';
        }else if($cate=="其他中小学/中职/特教学校"){
            $title[0] = '教委审核';
            $title[1] = '中心审核';
            $ntitle[0] = '区教育局审核';
            $ntitle[1] = '市中心审核';
        }else if($cate=="直属校"){
            
            $title[0] = '学校审核';
            $title[1] = '中心审核';
            $ntitle[0] = '学校审核';
            $ntitle[1] = '市中心审核';
        }
        $list = $list->toArray();
        $new_ary = [];
        // dump($title);die;
        foreach($list['data'] as $key=>$val){
            $new_ary[$key] = $val;
            foreach($title as $k=>$v){
                // echo $val['status'];die;
                $new_ary[$key]['statusdes'][$k] = $this->getstatus($v,$val['status']);
            }
        }
        // dump($new_ary);die;
        // $title[0] = '一级审核';
        // $title[1] = '二级审核';
        $this->assign('title',$ntitle);
        $this->assign('cate',$cate);
        $this->assign('data',$new_ary);
        return $this->fetch();
    }
  
  private function getstatus($type,$status){
      $des = '';
      if($type == '学校审核'){
          switch ($status) {
              case '1':
                  // code...
                  $des = '待审核';
                  break;
              case '2':
                  // code...
                  $des = '拒绝';
                  break;
              case '3':
                  // code...
                  $des = '审核通过';
                  break;
              case '4':
                  // code...
                  $des = '驳回';
                  break;
              case '5':
                  // code...
                  $des = '驳回';
                  break;
              case '6':
                  // code...
                  $des = '审核通过';
                  break;
              case '7':
                  // code...
                  $des = '审核通过';
                  break;
              default:
                  // code...
                  break;
          }
      }else if($type=="教委审核"){
          switch ($status) {
              case '1':
                  // code...
                  $des = '驳回';
                  break;
              case '2':
                  // code...
                  $des = '驳回';
                  break;
              case '3':
                  // code...
                  $des = '审核通过';
                  break;
              case '4':
                  // code...
                  $des = '待审核';
                  break;
              case '5':
                  // code...
                  $des = '驳回';
                  break;
              case '6':
                  // code...
                  $des = '审核通过';
                  break;
              case '7':
                  // code...
                  $des = '审核通过';
                  break;
              default:
                  // code...
                  break;
          }          
      }else if($type=="中心审核"){
          switch ($status) {
              case '1':
                  // code...
                  $des = '待审核';
                  break;
              case '2':
                  // code...
                  $des = '待审核';
                  break;
              case '3':
                  // code...
                  $des = '待审核';
                  break;
              case '4':
                  // code...
                  $des = '待审核';
                  break;
              case '5':
                  // code...
                  $des = '待审核';
                  break;
              case '6':
                  // code...
                  $des = '审核通过';
                  break;
              case '7':
                  // code...
                  $des = "驳回";
                  break;
              default:
                  // code...
                  break;
          }  
      }
      return $des;
  }

    public function edit(){
        $id = input("id");
        if(Request::isPost()){
            // 
            $data = input("post.");
            $id = input("id");
            unset($data['id']);
            $info = Db::name("shenqing")->where("id",$id)->find();
            if($info['if_re']==1){
                ajaxMsg(0,'您的申请，禁止被再次提交');
            }
            // dump($this->admin);die;
            // $kt_info = Db::name("keti")->where("id",$data['cid'])->find();
            //生成word
            $data_sq['if_inportant'] = $data['if_inportant'];
            $data_sq['content'] = json_encode($data);
            $data_sq['time'] = time();
            $data_sq['name'] = $data['name'];
            $data_sq['sex'] = $data['sex'];
            $data_sq['zhiwu'] = $data['zhiwu'];
            $data_sq['bumen'] = $data['bumen'];
            $data_sq['if_teacher'] = $data['if_teacher'];
            $data_sq['if_two'] = $data['if_two'];
            //判断所属学校,进行审批提交
             //1 学校待审核 2 学校拒绝 3 中心待审核 4 教委待审核 5 教委拒绝 6 审核通过 7 中心审核拒绝 
            if($this->admin['area']==52){
                $cate = "直属校";
                $data_sq['status'] = 1;
                
            }elseif($this->admin['area']==2){
                $cate = "普通高校、高职院校";
                $data_sq['status'] = 1;
            }else{
                $cate = "其他中小学/中职/特教学校";
                $data_sq['status'] = 4;
            }
            $data_sq['school_id'] = $this->admin['school'];


            $baocun = input("baocun");
            if($baocun==1){
                $data_sq['status'] = -1;
            }                  
            $data_sq['admin_id'] = $this->admin['id'];
            // dump($data_sq);die;
            // echo $id;die;
            $res = Db::name('shenqing')->where("id",$id)->update($data_sq);
         
            if($res){
              
                if($baocun==1){
                   return  ajaxMsg(1,'保存成功');
                    
                }else{
                   return ajaxMsg(1,'提交成功');
                    
                }
            }
            return   ajaxMsg(0,'网络错误,稍后再试');
            // echo 123;die;
        }
        
        $info = Db::name('shenqing')->where("admin_id",$this->admin['id'])->where('id',$id)->find();
        $this->assign("info",$info);
        $content = json_decode($info['content'],true);
        // dump($content);die;
        if(!isset($content['sannianchengguo'])){
            $content['sannianchengguo'] = '';
        }
        $content['s_name'] = isset($content['s_name'])?array_values($content['s_name']):'';
        $content['s_num'] =isset($content['s_num'])?array_values($content['s_num']):'';
        $content['s_price'] = isset($content['s_price'])?array_values($content['s_price']):'';
        $content['s_year'] = isset($content['s_year'])?array_values($content['s_year']):'';
        $content['member_name'] = isset($content['member_name'])?array_values($content['member_name']):'';
        $content['member_zhiye'] = isset($content['member_zhiye'])?array_values($content['member_zhiye']):'';
        $content['member_year'] = isset($content['member_year'])?array_values($content['member_year']):'';
        $content['member_zhuanye'] = isset($content['member_zhuanye'])?array_values($content['member_zhuanye']):'';
        $content['member_danwei'] = isset($content['member_danwei'])?array_values($content['member_danwei']):'';
        $content['member_fengong'] = isset($content['member_fengong'])?array_values($content['member_fengong']):'';
        $content['cate_title'] = isset($content['cate_title'])?array_values($content['cate_title']):'';
        $content['class_title'] = isset($content['class_title'])?array_values($content['class_title']):'';
        $content['p_time'] = isset($content['p_time'])?array_values($content['p_time']):'';
        $content['if_finish'] = isset($content['if_finish'])?array_values($content['if_finish']):'';
        $content['result'] = explode(',',$content['result']);
        // dump($content);die;
        $this->assign('content',$content);
        $this->assign("if_two",$info['if_two']);
        return $this->fetch();
    }
    public function shenqing_changestatus(){
        $id = input("id");
            if($this->admin['area']==52){
                $cate = "直属校";
                $data_sq['status'] = 1;
                
            }elseif($this->admin['area']==2){
                $cate = "普通高校、高职院校";
                $data_sq['status'] = 1;
            }else{
                $cate = "其他中小学/中职/特教学校";
                $data_sq['status'] = 4;
            }
        $info = Db::name("shenqing")->where("id",$id)->find();
        if($info['if_re']==1){
            ajaxMsg(0,'您的申请，禁止被再次提交');
        }
        if($res = Db::name('shenqing')->where('admin_id',$this->admin['id'])->where('cid',$info['cid'])->where('id','<>',$id)->where('status','not in','2,5,7')->find()){
             ajaxMsg(1,'您有其他的申请正在审核中');
        }
        $res = Db::name("shenqing")->where("id",$id)->update($data_sq);
        if($res){
            ajaxMsg(1,'申请成功');
        }
        ajaxMsg(0,'网络错误');
    }
    public function chakan(){
        $id = input('id');
        $type = input('type');
        $info = Db::name('shenqing')->where('id',$id)->find();
        $this->assign("info",$info);
        $content = json_decode($info['content'],true);
        $content['s_name'] = isset($content['s_name'])?array_values($content['s_name']):'';
        $content['s_num'] =isset($content['s_num'])?array_values($content['s_num']):'';
        $content['s_price'] = isset($content['s_price'])?array_values($content['s_price']):'';
        $content['s_year'] = isset($content['s_year'])?array_values($content['s_year']):'';
        $content['member_name'] = isset($content['member_name'])?array_values($content['member_name']):'';
        $content['member_zhiye'] = isset($content['member_zhiye'])?array_values($content['member_zhiye']):'';
        $content['member_year'] = isset($content['member_year'])?array_values($content['member_year']):'';
        $content['member_zhuanye'] = isset($content['member_zhuanye'])?array_values($content['member_zhuanye']):'';
        $content['member_danwei'] = isset($content['member_danwei'])?array_values($content['member_danwei']):'';
        $content['member_fengong'] = isset($content['member_fengong'])?array_values($content['member_fengong']):'';
        $content['cate_title'] = isset($content['cate_title'])?array_values($content['cate_title']):'';
        $content['class_title'] = isset($content['class_title'])?array_values($content['class_title']):'';
        $content['p_time'] = isset($content['p_time'])?array_values($content['p_time']):'';
        $content['if_finish'] = isset($content['if_finish'])?array_values($content['if_finish']):'';
        $content['sannianchengguo'] = isset($content['sannianchengguo'])?$content['sannianchengguo']:'';
        // dump($content);die;
        $this->assign('content',$content);
        $this->assign("if_two",$info['if_two']);
        

        return $this->fetch();
    }
}

// 1 学校待审核 2 学校拒绝 3 中心待审核 4 教委待审核 5 教委拒绝 6 审核通过 7 中心审核拒绝 
















